﻿Imports Microsoft.VisualBasic
Imports System.Collections


Public Class Mazo
    Inherits ArrayList

    Private baraja As Baraja


    Public Sub New(ByVal barajaB As Baraja)
        Me.baraja = barajaB
    End Sub

    Public Sub New(ByVal m As Mazo)
        AddRange(m)
        baraja = m.getBaraja()
    End Sub

    Public Sub New(ByVal descripcion As String, ByVal baraja As Baraja)
        Me.baraja = baraja
        Dim cadena As String = descripcion
        cadena = cadena.Trim("{")
        cadena = cadena.Trim("}")
        cadena = cadena.Trim(",")
        While cadena <> ""
            Dim nueva As Carta = baraja.cartaParaId(cadena.Substring(0, cadena.IndexOf(" ")))
            cadena = cadena.Remove(0, cadena.IndexOf(" "))
        End While
    End Sub

    Public Sub mezcla()
        Randomize()
        Dim rnd As Random
        'Me.AddRange(rnd.NextBytes)
    End Sub

    Public Sub ordena()
        Me.Sort()

    End Sub

    Public Function cartaEn(ByVal i As Integer) As Carta
        Return Me(i)
    End Function

    Public Function puedeSacar(ByVal aSacar As Mazo) As Boolean
        Dim copia As Mazo = New Mazo(Me)
        Return copia.saca(aSacar)
    End Function
    Public Function saca(ByVal aSacar As Mazo) As Boolean
        Dim total As Integer = 0
        For Each Carta As DictionaryEntry In aSacar
            For i As Integer = 0 To Me.Count
                If cartaEn(i).Equals(Carta) Then
                    total = total + 1
                    Me.Remove(i)
                    Exit For
                End If
            Next i
        Next
        Return total.Equals(aSacar.Count)
    End Function

    Public Function sacaPrimera() As Carta
        Dim carta As Carta = Me(0)
        Me.Remove(0)
        Return carta
    End Function

    Public Overloads Function toString() As String
        Dim cadena As String
        cadena = "{"
        For i As Integer = 0 To Me.Count - 1
            cadena = cadena & cartaEn(i).pID
            If i + 1 < Me.Count - 1 Then
                cadena = cadena & ", "
            End If
        Next i
        cadena = cadena + "}"
        Return cadena
    End Function


    Public Function getBaraja() As Baraja
        Return baraja
    End Function

    Public Function cuenta(ByVal c As Carta) As Integer
        Dim total As Integer = 0
        For Each Carta As DictionaryEntry In Me
            If c.Equals(Carta) Then
                total += 1
            End If
        Next
    End Function
End Class
